<?php

class PostController extends Controller
{
	public function init(){
		parent::init();
		$cs = Yii::app()->clientScript;
		$cs->registerScriptFile('/js/jquery.js');
	}
	public function actionIndex()
	{
		$posts = Post::model()->allPosts();
		$this->render('index', array('posts'=>$posts));
	}
	public function actionContent($id)
	{
		$post = Post::model()->findByPk($id);
		echo $post['content'];
	}
	public function actionAdd($store_id)
	{
		$this->layout = nil;
		$store = Store::model()->findByPk($store_id);
    $model=new Post;
    $model->user_id = $store->user_id;
    
    if(isset($_POST['Post']))
    {
        $model->attributes=$_POST['Post'];
        $model->image_url = CUploadedFile::getInstance($model,'image_url');
        if($model->save())
        {
          $path = 'images/post/'.$model->id.'/';
					mkdir($path);
      		$model->image_url->saveAs($path.'big_'.$model->image_url->getName());
					$image = Yii::app()->image->load($path.'big_'.$model->image_url->getName());
					$image->resize(80,80);
					$image->save($path.$model->image_url->getName());
					
					$this->redirect(array('index','name'=>$store->name));
        }
    }
    $this->render('add',array('model'=>$model));
}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}